package org.tis.tools.senior.module.developer.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.senior.module.core.web.util.ShiroUtil;
import org.tis.tools.senior.module.developer.controller.request.AgainDeliveryRequest;
import org.tis.tools.senior.module.developer.controller.request.RepealAddRequest;
import org.tis.tools.senior.module.developer.entity.SRepeal;
import org.tis.tools.senior.module.developer.entity.SRepealList;
import org.tis.tools.senior.module.developer.entity.SSvnAccount;
import org.tis.tools.senior.module.developer.entity.vo.RepealDetail;
import org.tis.tools.senior.module.developer.service.ISRepealService;

import javax.validation.constraints.NotNull;
import java.text.ParseException;

/**
 * sRepeal的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/09/26
 */
@RestController
@RequestMapping("/sRepeal")
public class SRepealController extends BaseController<SRepeal>  {

    @Autowired
    private ISRepealService sRepealService;

    /**
     * 查询再次投放的可投放窗口
     * @param guid
     * @return
     */
    @ApiOperation(value = "查询再次投放的可投放窗口")
    @GetMapping("/{guid}/packTimeVerify")
    public ResultVO packTimeVerify(@PathVariable
                                   @ApiParam(name = "guid", value = "延期申请guid", required = true)
                                   @NotNull(message = "延期申请guid不能为空")
                                           Integer guid) throws ParseException {

        return ResultVO.success("查询成功", sRepealService.selectPackTime(guid));
    }

    /**
     * 新增延期申请
     * @param request
     * @return
     */
    @ApiOperation(value = "新增延期申请")
    @PostMapping
    public ResultVO add(@RequestBody
                            @ApiParam(name = "request", value = "延期申请信息")
                            @Validated
                                    RepealAddRequest request) {

        SSvnAccount user = ShiroUtil.getUser();
        sRepealService.addRepealApply(user.getUserId(), request);
        return ResultVO.success("新增成功！");
    }

    /**
     * 查询所有延期投产申请
     * @param page
     * @return
     */
    @ApiOperation(value = "查询所有延期投产申请")
    @PostMapping("/all")
    public ResultVO repealAll(@RequestBody
                                    @ApiParam(name = "page", value = "分页对象", required = true)
                                    @Validated
                                            SmartPage<RepealDetail> page){
        Page<RepealDetail> sStandardListPage = new Page<>
                (page.getPage().getCurrent(), page.getPage().getSize(),
                        page.getPage().getOrderByField(), page.getPage().getAsc());

        return ResultVO.success("查询成功", sRepealService.selectRepealAll(
                sStandardListPage, getWrapper(page.getCondition())));
    }

    /**
     * 将延期申请状态置为延期成功
     * @return
     */
    @ApiOperation(value = "修改延期申请状态为成功")
    @PutMapping("/{guid}")
    public ResultVO affirmSeccess(@PathVariable
                                      @ApiParam(name = "guid", value = "延期申请guid", required = true)
                                      @NotNull(message = "延期申请guid不能为空")
                                              Integer guid){
        SSvnAccount user = ShiroUtil.getUser();
        sRepealService.affirmRepealSeccess(user.getUserId(), guid);
        return ResultVO.success("延期成功");
    }

    /**
     * 删除延期申请
     * @param guid
     * @return
     */
    @ApiOperation(value = "删除延期申请")
    @DeleteMapping("/{guid}")
    public ResultVO deleteRepeal(@PathVariable
                                 @ApiParam(name = "guid", value = "延期申请guid", required = true)
                                 @NotNull(message = "延期申请guid不能为空")
                                         Integer guid){
        sRepealService.deleteRepeal(guid);
        return ResultVO.success("删除申请");
    }

    /**
     * 查询再次投放延期申请的详情
     * @param page
     * @return
     */
    @ApiOperation(value = "查询再次投放延期申请的详情")
    @PostMapping("/{guid}/repealListDetail")
    public ResultVO repealListDetail(@PathVariable
                                         @ApiParam(name = "guid", value = "延期申请guid", required = true)
                                         @NotNull(message = "延期申请guid不能为空")
                                                 Integer guid,
                                     @RequestBody
                                        @ApiParam(name = "page", value = "分页对象", required = true)
                                        @Validated
                                      SmartPage<SRepealList> page){
        Page<SRepealList> sStandardListPage = new Page<>
                (page.getPage().getCurrent(), page.getPage().getSize(),
                        page.getPage().getOrderByField(), page.getPage().getAsc());

        return ResultVO.success("查询成功", sRepealService.selectRepealListDetail(
                guid, sStandardListPage, getWrapper(page.getCondition())));
    }

    /**
     * 延期投产申请再次投放
     * @param request
     * @return
     */
    @ApiOperation(value = "延期投产申请再次投放")
    @PostMapping("/againDelivery")
    public ResultVO againDelivery(@RequestBody
                                      @ApiParam(name = "request", value = "生成投放申请信息")
                                      @Validated
                                          AgainDeliveryRequest request) throws ParseException {
        SSvnAccount user = ShiroUtil.getUser();
        sRepealService.againDelivery(user.getUserId(), request);
        return ResultVO.success("投放成功");
    }

    /**
     * 查询此延迟投产申请详情
     * @return
     */
    @ApiOperation(value = "查询此延迟投产申请详情")
    @GetMapping("/{guidRepeal}/repealListDetail")
    public ResultVO RepealListDetail(@PathVariable
                                     @ApiParam(name = "guid", value = "延期申请guid", required = true)
                                     @NotNull(message = "延期申请guid不能为空")
                                             Integer guidRepeal){
        return ResultVO.success("查询成功",sRepealService.selectRepealListDetail(guidRepeal));
    }

}

